package com.hmdp.utils;

import org.springframework.web.servlet.HandlerInterceptor;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



/**
 *  用户登录拦截器
 */
public class LoginInterceptor implements HandlerInterceptor {

    /**
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 1.判断用户是否存在
        if (UserHolder.getUser() == null) {
            // 不存在，拦截，返回401状态码
            response.setStatus(401);
            return false;
        }
        // 放行
        return true;
    }
}
